import irclib
import os
import time
import random
import threading
irclib.DEBUG = True
# Submit a question to lolita3dbot with programd via irc
# To place in a script at the beginning of the game
# we have to connect one time not at each request
# Connection information
#network = 'irc.europnet.org'
network='localhost'
port = 6667
channel = '#lolita3d'
# All clients must have a different name
nick = 'ctcp' + str(random.randint(1, 100)) + str(random.randint(1,100))
name = 'ctcp'

# Generic echo handler ( no space added )
def handlequit ( connection, event ):
	server.connect ( network, port, nick, ircname = name )
	server.join ( channel )
	print 'reconnect... joy !'

# Using ping to send message was a terrible idea.
# You just have to use a timer ! tsk !
def MyTimer(tempo = 1.0):
        threading.Timer(tempo, MyTimer, [tempo]).start()
        ## verif the timer
        os.system('date')
        print 'here is a ctcp timer philosophy...'
        # Send a ping to the channel to create ctcp event        
        # server.privmsg ('#lolita3d','/PING #lolita3d')
	server.ctcp('PING','#lolita3d')

# Create the IRC object
irc = irclib.IRC()

# Connect to the network
server = irc.server()
server.connect ( network, port, nick, ircname = name )
server.join ( channel )

# Activate timer to send ctcp
MyTimer(4.0)

